import { useReactive } from "ahooks";
import { useModel } from "umi";
import { useEffect } from "react";
import compress from "gql-compress";

const useStaff = ({ osType, osStat }) => {
    const { graphqlFetch } = useModel("global");

    const state = useReactive({
        loading: false,
        staffs: [],
    });

    const loadStaffs = async () => {
        state.loading = true;
        let res = await graphqlFetch({
            query,
            variables: {
                osType,
                osStat,
            },
        });
        state.staffs = res?.a?.nodes || [];
        state.loading = false;
    };

    useEffect(() => {
        loadStaffs();
    }, []);

    return { ...state, loadStaffs };
};

const query = compress(`query MyQuery($osStat: Boolean = true, $osType: String) {
    a: allOrganizationStaffs(condition: { osStat: $osStat, osType: $osType }) {
        nodes {
            deInfo
            isBoss
            omUuid
            opUuid
            osBirt
            osFrom
            osIdnu
            osName
            osNati
            osPhon
            osRoge
            osStat
            osType
            osUuid
            position: organizationPositionByOpUuid {
                opName
            }
        }
    }
}`);

export default useStaff;
